from urllib.request import urlopen
import requests
import os
from bs4 import BeautifulSoup


# 仅仅下载指导页面的所有图片
# 创建文件夹
def create_dir(name):
    if not os.path.exists(name):
        os.makedirs(name)


# 循环获取当前页面的图片,并保存
def get_pic(pic_list):
    for i in pic_list:
        link = i.get('file')
        headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0"}
        r = requests.get(link, headers)  # 下载图片，之后保存到文件
        with open('single/{}'.format(link.split('/')[-1]), 'wb') as f:
            f.write(r.content)


# 主方法
def main():
    create_dir('single')
    link = 'https://www.chongbuluo.com/thread-6030-1-1.html'
    soup = BeautifulSoup(urlopen(link), 'html.parser')
    pic_list = soup.find('div', 'pcb').find('div', 't_fsz').find('td', 't_f').find_all('img')
    get_pic(pic_list)


if __name__ == '__main__':
    main()
